<template>
  <van-datetime-picker
  v-model="currentDate"
  type="date"
  title="选择年月日"
  :min-date="minDate"
  :max-date="maxDate"
  @cancel="$emit('close')"
  @confirm='updateBirthday'
/>
</template>

<script>
import { updateUserProfile } from "@/api/user";
import dayjs from 'dayjs'
export default {
    props:{
        value:{
            type:[String,Number],
            required:true
        }
    },
  data() {
    return {
      minDate: new Date(1970, 0, 1),
      maxDate: new Date(),
      currentDate: new Date(this.value),
    };
  },
  methods:{
     async updateBirthday(){
        this.$toast.loading({
            message:'保存中.......',
            forbidClick:true //禁止点击
        })
        try{
            const currentDate=dayjs(this.currentDate).format('YYYY-MM-DD')
            await updateUserProfile({
                birthday: currentDate
            })
            console.log(this.currentDate,  dayjs(this.currentDate).format("YYYY-MM-DD"))

       
            // 更新视图
            this.$emit('input',  dayjs(this.currentDate).format("YYYY-MM-DD"))
            // 关闭弹层
            this.$emit('close')
            // 提示成功
            this.$toast.success('更新成功')
        }catch(err){
            // 更新失败
            this.$toast.fail('更新失败')
            console.log(err)
        }
    }
  }
};
</script>

<style>

</style>